package com.hemerasolutions.v2trainer.component.list;

import java.util.HashMap;
import java.util.Map;

import javax.faces.event.ValueChangeEvent;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

import com.hemerasolutions.v2trainer.component.BaseComponent;
import com.hemerasolutions.v2trainer.component.interfaces.list.IntensityZoneListCompInterface;
import com.hemerasolutions.v2trainer.component.interfaces.table.IntensityZoneTableCompInterface;
import com.hemerasolutions.v2trainer.model.exercise.Exercise;
import com.hemerasolutions.v2trainer.model.exercise.VersionableExercise;
import com.hemerasolutions.v2trainer.model.exercise.intensity.IntensityExecution;
import com.hemerasolutions.v2trainer.model.exercise.intensity.IntensityTable;
import com.hemerasolutions.v2trainer.model.modality.Modality;

@Controller
@Component("intensityZoneListComp")
@Scope("prototype")
public class IntensityZoneListComp extends BaseComponent implements IntensityZoneListCompInterface {

	private static final long serialVersionUID = 1L;

	private VersionableExercise versionableExercise;
	
	private Exercise exercise;
	
	private Map<Long,IntensityZoneTableCompInterface> intensityZoneTables;
	
	private Modality modality;
	
	private IntensityZoneTableCompInterface selIntensityZoneTable;
	
	private IntensityTable fromIntensityTable;
	
	private IntensityExecution selIntensityExecution;
	
	private Exercise selExercise;
	
	@Autowired
	private transient ApplicationContext ctx;
	
	@Override
	public void refresh() {
		
		this.exercise.prepareIntensitys(this.modality.getIntensityModTables());
		this.loadIntensityZoneTables();
	}


	
	@Override
	public void onVersionChanged(ValueChangeEvent e){
		this.refresh();
	}	

	
	private void loadIntensityZoneTables(){

		this.intensityZoneTables = new HashMap<Long,IntensityZoneTableCompInterface>();

		for(IntensityExecution ie:this.exercise.getIntensityExecutions()){
			for(IntensityTable it:ie.getIntensityTables()){

				IntensityZoneTableCompInterface iztHandler = ctx.getBean("intensityZoneTableComp",IntensityZoneTableCompInterface.class); ;
				
				iztHandler.setModality(this.modality);
				iztHandler.setIntensityExecution(ie);
				iztHandler.setIntensityTable(it);
				iztHandler.refresh();
				
				intensityZoneTables.put(it.getUuid(), iztHandler);
				
			}
		}
	}
	
    @Override
	public void copyZones(){
    	
		for(IntensityExecution ie: this.selExercise.getIntensityExecutions()){
			if(this.exercise.getExecutions().contains(ie.getExecution())){
				IntensityExecution ieAux = new IntensityExecution(ie,this.exercise);
				
				for(IntensityExecution ieAux2: this.exercise.getIntensityExecutions()){
					if(ieAux2.getExecution().equals(ieAux.getExecution())){
						this.exercise.getIntensityExecutions().remove(ieAux2);
						break;
					}
				}
				
				
				this.exercise.getIntensityExecutions().add(ieAux);
			}
		}    	
    	
    	
    	this.loadIntensityZoneTables();
    
    }
    	
	
	@Override
	public void copyLevelIntensity(){

		if(this.selIntensityZoneTable.zonesCanBeChanged()){
			this.selIntensityZoneTable.getIntensityTable().copyIntensityTable(this.fromIntensityTable);
			this.selIntensityZoneTable.refresh();
		}else{
			this.showErrorMessage("ErrorOnReplicate", this.localizedText("Level") + " " +
									this.localizedText(this.selIntensityZoneTable.getIntensityTable().getLevelName()) + " " +
									this.localizedText("cantBeChanged"));
			this.selIntensityZoneTable.showUsedZoneError();
		}
		
	}

	@Override
	public void replicateGeneral(){
    	for(IntensityZoneTableCompInterface iztHandler:this.intensityZoneTables.values()){
    		if( iztHandler.getIntensityTable().getUuid() != this.fromIntensityTable.getUuid() &&
   				iztHandler.getIntensityExecution() == this.selIntensityExecution){
				this.selIntensityZoneTable = iztHandler;
				this.copyLevelIntensity();
    		}
    	}
	}


	@Override
	public Exercise getExercise() {
		return exercise;
	}


	@Override
	public void setExercise(Exercise exercise) {
		this.exercise = exercise;
	}


	@Override
	public Map<Long, IntensityZoneTableCompInterface> getIntensityZoneTables() {
		return intensityZoneTables;
	}


	@Override
	public void setIntensityZoneTables(
			Map<Long, IntensityZoneTableCompInterface> intensityZoneTables) {
		this.intensityZoneTables = intensityZoneTables;
	}


	@Override
	public Modality getModality() {
		return modality;
	}


	@Override
	public void setModality(Modality modality) {
		this.modality = modality;
	}


	@Override
	public IntensityZoneTableCompInterface getSelIntensityZoneTable() {
		return selIntensityZoneTable;
	}


	@Override
	public void setSelIntensityZoneTable(
			IntensityZoneTableCompInterface selIntensityZoneTable) {
		this.selIntensityZoneTable = selIntensityZoneTable;
	}


	@Override
	public Exercise getSelExercise() {
		return selExercise;
	}


	@Override
	public void setSelExercise(Exercise selExercise) {
		this.selExercise = selExercise;
	}


	@Override
	public IntensityExecution getSelIntensityExecution() {
		return selIntensityExecution;
	}


	@Override
	public void setSelIntensityExecution(IntensityExecution selIntensityExecution) {
		this.selIntensityExecution = selIntensityExecution;
	}


	@Override
	public IntensityTable getFromIntensityTable() {
		return fromIntensityTable;
	}


	@Override
	public void setFromIntensityTable(IntensityTable fromIntensityTable) {
		this.fromIntensityTable = fromIntensityTable;
	}


	@Override
	public VersionableExercise getVersionabeExercise() {
		return versionableExercise;
	}


	@Override
	public void setVersionabeExercise(VersionableExercise versionabeExercise) {
		this.versionableExercise = versionabeExercise;
	}

	
}
